﻿/***原型模式：主要是在克隆函数中使用拷贝构造函数
 *            继承 + clone()函数实现对象的拷贝
 */
#include <iostream>
#include <string>
using namespace std;
// 原型接口类
class AbSoldier
{
public:
  string name;
  AbSoldier(const string &name_)
  {
    name = name_;
  }
  virtual AbSoldier *clone() = 0; // 克隆函数，复制对象
  void WhoAmI()
  {
    cout << "I am " << name << endl;
  }
};
class Soldier01 : public AbSoldier
{
public:
  using AbSoldier::name;
  Soldier01(const string &name_) : AbSoldier(name_) {}
  Soldier01(const Soldier01 &obj) : AbSoldier(obj.name) {}
  AbSoldier *clone()
  {
    return new Soldier01(*this); // 此处调用拷贝构造函数，以实现对象的复制
  }
};
class Soldier02 : public AbSoldier
{
public:
  Soldier02(const string &name_) : AbSoldier(name_) {}
  AbSoldier *clone()
  {
    return new Soldier02(*this);
  }
};
int main()
{
  AbSoldier *soldier_one = new Soldier01("士兵01");
  AbSoldier *soldier_one02 = soldier_one->clone();
  soldier_one02->WhoAmI();
  delete soldier_one;
  soldier_one == nullptr;
  delete soldier_one02;
  soldier_one02 = nullptr;
  return 0;
}